package com.boss.salary.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.DateUtils;
import com.boss.config.FileServiceConfiguration;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.*;
import com.boss.salary.entity.*;
import com.boss.salary.service.ISalaryBankPayBackService;
import com.boss.salary.vo.Offer;
import com.boss.sys.utils.SysUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@Service
public class SalaryBankPayBackServiceImpl implements ISalaryBankPayBackService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(PersonSalaryInfoServiceImpl.class);

    /**
     * 引入接口服务配置类
     */
    @Autowired
    private FileServiceConfiguration fileServiceConfiguration;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ISalaryBankPayBackDao iSalaryBankPayBackDao;
    @Autowired
    private IPersonExtDao iPersonExtDao;
    @Autowired
    private IPersonExtSubmitDao personExtSubmitDao;
    @Autowired
    private IPayeeAcctDao payeeAcctDao;
    @Autowired
    private IPersonOperateDao iPersonOperateDao;
    @Autowired
    private IAgencyVerifyDao iAgencyVerifyDao;

    @Autowired
    private IFilePathDao filePathDao;
    @Autowired
    private ICommonService commonService;
    @Override
    public PageDTO<SalaryBankPayBack> querySalaryBankPayBackPage(PaginationDTO paginationDTO, Map<String, Object> queryParam, List<String> childrenId) {
        List<SalaryBankPayBack> resultList = new ArrayList<>();
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.querySalaryBankPayBackPage(queryParam,childrenId);
        List<SalaryBankPayBack> listTal = iSalaryBankPayBackDao.querySalaryBankPayBackTotal(paginationDTO, queryParam,childrenId);
       /* if (list.size() > 0) {
            int no = 0;
            String oldAgencyCode = "";
            List<SalaryBankPayBack> childrenList = new ArrayList<>();
            for (SalaryBankPayBack salaryBankPayBack : list) {
                String agencyCode = salaryBankPayBack.getAgencyCode();
                if (oldAgencyCode.equals(agencyCode)) {
                    resultList.get(resultList.size() - 1 ).getChildren().add(salaryBankPayBack);
                } else {
                    if (resultList.size() > 0) {
                        childrenList = new ArrayList<>();
                    }
                    oldAgencyCode = agencyCode;
                    ++no;
                    SalaryBankPayBack salaryBankPayBack1 = new SalaryBankPayBack();
                    salaryBankPayBack1.setAgencyName(salaryBankPayBack.getAgencyName());
                    salaryBankPayBack1.setNo(String.valueOf(no));
                    salaryBankPayBack1.setId(String.valueOf(no));
                    salaryBankPayBack1.setAgencyId(salaryBankPayBack.getAgencyId());
                    childrenList.add(salaryBankPayBack);
                    salaryBankPayBack1.setChildren(childrenList);
                    resultList.add(salaryBankPayBack1);
                }
            }
        }*/
//        int total = iSalaryBankPayBackDao.querySalaryBankPayBackTotal(queryParam);
        Integer no=1;

        if(list.size()>0){
            for (SalaryBankPayBack bankPayBack : listTal) {
                bankPayBack.setNo(no.toString());
                bankPayBack.setId(no.toString());
                bankPayBack.setChildren(new ArrayList<>());
                no++;
                String agencyCode = bankPayBack.getAgencyCode();

                for (SalaryBankPayBack salaryBankPayBack : list) {
                    if(agencyCode.equals(salaryBankPayBack.getAgencyCode())){
                        bankPayBack.getChildren().add(salaryBankPayBack);
                    }
                }
            }
        }
        PageDTO<SalaryBankPayBack> pageDTO = new PageDTO<>();
        pageDTO.setRows(listTal);
        pageDTO.setTotal(listTal.size());
        return pageDTO;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendCardNoToAgency(List<String> agencyIds) {
        iSalaryBankPayBackDao.sendCardNoToAgency(agencyIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String cancelSendCard(List<String> agencyIds) {
        // 1. 获取当前单位下异常卡号人员的状态
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.getAgencyVerifyState(agencyIds);
        // 2. 如果财政以确认或审核，则不允许重新下发
        boolean isAudit = false;
        if (list.size() > 0) {
            for (SalaryBankPayBack salaryBankPayBack : list) {
                if (SystemDictConstant.IS_SUBMIT.equals(salaryBankPayBack.getVerifiedCode()) ||
                SystemDictConstant.IS_AUDIT.equals(salaryBankPayBack.getVerifiedCode())) {
                    isAudit = true;
                    break;
                }
            }
        }
        // 3. 如果财政未确认或未审核，修改下发状态
        if (!isAudit) {
            iSalaryBankPayBackDao.cancelSendCard(agencyIds);
        } else {
            return "500";
        }
        return "200";
    }

    @Override
    public PageDTO<SalaryBankPayBack> querySalaryBankPayBackSendPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.querySalaryBankPayBackSendPage(paginationDTO, queryParam);
        int total = iSalaryBankPayBackDao.querySalaryBankPayBackSendTotal(queryParam);
        PageDTO<SalaryBankPayBack> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBankInfo(SalaryBankPayBack salaryBankPayBack) {
        if(salaryBankPayBack.getIsUpdate()==0){
            PersonExt personExt= iPersonExtDao.queryPersonExtByIden(salaryBankPayBack.getIdenNo(),salaryBankPayBack.getMofDivCode());
            salaryBankPayBack.setPersonId(personExt.getPersonId());
        }
        salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryBankPayBack.setVerifiedInAgencyCode(SystemDictConstant.IS_AUDIT);
        salaryBankPayBack.setIsUpdate(SystemDictConstant.IS_TRUE);
        //通过PersonID获取personEXT表中的人员信息
        PersonExt personExt = iPersonExtDao.selectOnePersonByPersonId(salaryBankPayBack.getPersonId());
        //通过MofDivCode(区划)和AgencyId(单位)在AgencyVerify表中查询最大SalaryMonth
        String maxSalaryMonth = iAgencyVerifyDao.getSalaryMonth(personExt.getMofDivCode(),salaryBankPayBack.getAgencyId());
        PersonOperate personOperate = new PersonOperate();
        //将personExt实体类的数据插入到personOperate实体类中
        BeanUtils.copyProperties(personExt,personOperate);
        personOperate.setSalaryMonth(maxSalaryMonth);
        personOperate.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personOperate.setIdenNo(salaryBankPayBack.getNewIdenNo());
        personOperate.setSalaCardNo(salaryBankPayBack.getNewSalaCardNo());
        personOperate.setPersonName(salaryBankPayBack.getNewPersonName());
        personOperate.setOperateCode("edit");
        //自动生成ID
        personOperate.setId(SysUtils.generateGuid());
        //向personOperate表插入信息
        iPersonOperateDao.savePersonOperate(personOperate);

        iSalaryBankPayBackDao.updateSalaryBankPayBack(salaryBankPayBack);
        //更新人员信息新工资卡号
        iPersonExtDao.updateSalaCardNoByIdenNo(salaryBankPayBack);
        //更新人员上报信息新工资卡号
        personExtSubmitDao.updateSalaCardNoByIdenNo(salaryBankPayBack);
        //更新人员表
        iPersonExtDao.updatePersonInfoByIdenNo(salaryBankPayBack);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revokeUpdate(List<SalaryBankPayBack> list) {
        for (SalaryBankPayBack salaryBankPayBack : list) {
            salaryBankPayBack.setIsUpdate(SystemDictConstant.IS_FALSE);
            salaryBankPayBack.setIsLongUse(SystemDictConstant.IS_FALSE);
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iSalaryBankPayBackDao.revokeUpdate(salaryBankPayBack);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String verifyPass(SalaryBankPayBack salaryBankPayBack) {
        // 获取该单位下所有人员是否都已修改
        int total = iSalaryBankPayBackDao.getNotUpdateTotal(salaryBankPayBack.getAgencyId());
        if (total > 0) {
            return "500";
        }
        // 修改审核状态为审核通过
        salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryBankPayBack.setVerifiedInAgencyCode(SystemDictConstant.IS_AUDIT);
        iSalaryBankPayBackDao.updateVerifyByAgencyId(salaryBankPayBack);
        return "200";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String revokeVerify(SalaryBankPayBack salaryBankPayBack) {
        // 获取当前单位的财政审核状态，如果以送审或者以审核，不可以撤销审核
        List<String> agencyIds = new ArrayList<>(1);
        agencyIds.add(salaryBankPayBack.getAgencyId());
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.getAgencyVerifyState(agencyIds);
        String agencyVerifyCode = "";
        boolean isAudit = false;
        if (list.size() > 0) {
            for (SalaryBankPayBack salaryBankPayBack1 : list) {
                if (!StringUtils.isEmpty(salaryBankPayBack1.getVerifiedCode())) {
                    isAudit = true;
                    break;
                }
                if (!SystemDictConstant.IS_AUDIT.equals(salaryBankPayBack1.getVerifiedInAgencyCode())) {
                    agencyVerifyCode = salaryBankPayBack1.getVerifiedInAgencyCode();
                    isAudit = true;
                    break;
                }
            }
        }
        if (!isAudit) {
            // 修改审核状态为待审核
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            salaryBankPayBack.setVerifiedInAgencyCode(SystemDictConstant.IS_SUBMIT);
            iSalaryBankPayBackDao.updateVerifyByAgencyId(salaryBankPayBack);
        } else {
            // 单位审核未通过，不允许撤销
            if (!StringUtils.isEmpty(agencyVerifyCode)) {
                return "501";
            }
            // 已送审财政，不可以撤销
            return "500";
        }
        return "200";

    }
//暂时我要用
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String sendVerifyFinance(SalaryBankPayBack salaryBankPayBack) {
        // 获取当前单位的财政审核状态，如果单位已审核，可以送审财政
        List<String> agencyIds = new ArrayList<>(1);
        agencyIds.add(salaryBankPayBack.getAgencyId());
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.getAgencyVerifyState(agencyIds);
        boolean isAudit = false;
        if (list.size() > 0) {
            for (SalaryBankPayBack salaryBankPayBack1 : list) {
                if (!SystemDictConstant.IS_AUDIT.equals(salaryBankPayBack1.getVerifiedInAgencyCode())) {
                    isAudit = true;
                    break;
                }
            }
        }
        if (!isAudit) {
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            salaryBankPayBack.setVerifiedCode(SystemDictConstant.IS_SUBMIT);
            iSalaryBankPayBackDao.sendVerifyFinance(salaryBankPayBack);
        } else {
            return "500";
        }
        return "200";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String revokeVerifyFinance(SalaryBankPayBack salaryBankPayBack) {
        // 获取当前单位的财政审核状态，如果单位已送审财政并且财政未操作，可以撤销
        List<String> agencyIds = new ArrayList<>(1);
        agencyIds.add(salaryBankPayBack.getAgencyId());
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.getAgencyVerifyState(agencyIds);
        boolean isAudit = false;
        String verifyCode = "";
        if (list.size() > 0) {
            for (SalaryBankPayBack salaryBankPayBack1 : list) {
                if (!SystemDictConstant.IS_SUBMIT.equals(salaryBankPayBack1.getVerifiedCode())) {
                    verifyCode = salaryBankPayBack1.getVerifiedCode();
                    isAudit = true;
                    break;
                }
            }
        }
        if (!isAudit) {
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            salaryBankPayBack.setVerifiedCode(null);
            iSalaryBankPayBackDao.revokeVerifyFinance(salaryBankPayBack);
        } else {
            // 未提交财政返回错误
            if (StringUtils.isEmpty(verifyCode)) {
                return "501";
            }
            // 已提交财政，财政已操作，返回错误
            return "500";
        }
        return "200";
    }

    @Override
    public PageDTO<SalaryBankPayBack> queryFinanceVerifyPage(PaginationDTO paginationDTO, Map<String, Object> queryParam,List<String> childrenId) {

        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.queryFinanceVerifyPage(queryParam,childrenId);
        List<SalaryBankPayBack> listTal = iSalaryBankPayBackDao.queryFinanceVerifyTotal(paginationDTO, queryParam,childrenId);

        Integer no=1;

        if(list.size()>0){
            for (SalaryBankPayBack bankPayBack : listTal) {
                bankPayBack.setNo(no.toString());
                bankPayBack.setId(no.toString());
                bankPayBack.setChildren(new ArrayList<>());
                no++;
                String agencyCode = bankPayBack.getAgencyCode();

                for (SalaryBankPayBack salaryBankPayBack : list) {
                    if(agencyCode.equals(salaryBankPayBack.getAgencyCode())){
                        bankPayBack.getChildren().add(salaryBankPayBack);
                    }
                }
            }
        }
        PageDTO<SalaryBankPayBack> pageDTO = new PageDTO<>();
        pageDTO.setRows(listTal);
        pageDTO.setTotal(listTal.size());
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String financeVerifyConfirm(List<SalaryBankPayBack> salaryBankPayBacklist) {

        List<String> agencyIds = new ArrayList<>(1);
        for (SalaryBankPayBack salaryBankPayBack : salaryBankPayBacklist) {
            agencyIds.add(salaryBankPayBack.getAgencyId());
        }
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.getAgencyVerifyState(agencyIds);
        boolean isVerified_code= false;
        if(list.size()>0){
            for(SalaryBankPayBack salaryBankPayBack1:list){
                if(!SystemDictConstant.NO_SUBMIT.equals(salaryBankPayBack1.getVerifiedCode())){
                    isVerified_code=true;
                    break;
                }
            }
        }
        if(!isVerified_code){
            for (SalaryBankPayBack salaryBankPayBack : salaryBankPayBacklist) {

                salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                iSalaryBankPayBackDao.financeVerifyConfirm(salaryBankPayBack);
            }
        }else{
            return "500";
        }
          return "200";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String financeVerifySendBack(List<SalaryBankPayBack> list) {
        for (SalaryBankPayBack salaryBankPayBack : list) {
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iSalaryBankPayBackDao.financeVerifySendBack(salaryBankPayBack);
        }
               return "200";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String financeVerifyCancelConfirm(List<SalaryBankPayBack> list) {
        for (SalaryBankPayBack salaryBankPayBack : list) {
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iSalaryBankPayBackDao.financeVerifyCancelConfirm(salaryBankPayBack);
        }
             return "200";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String financeVerifyAuditPass(List<SalaryBankPayBack> list) {
        for (SalaryBankPayBack salaryBankPayBack : list) {
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iSalaryBankPayBackDao.financeVerifyAuditPass(salaryBankPayBack);
        }
            return "200";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String financeVerifyUndoAudit(List<SalaryBankPayBack> list) {
        for (SalaryBankPayBack salaryBankPayBack : list) {
            salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            iSalaryBankPayBackDao.financeVerifyUndoAudit(salaryBankPayBack);
        }
          return "200";
    }

    @Override
    public PageDTO<SalaryBankPayBack> queryCardNumberFiscalPage(PaginationDTO paginationDTO, Map<String, Object> queryParam, List<String> childrenId) {
        List<SalaryBankPayBack> list = iSalaryBankPayBackDao.queryCardNumberFiscalPage(paginationDTO,queryParam,childrenId);
        SalaryBankPayBack combined = iSalaryBankPayBackDao.getCardNumberFiscalCombined(queryParam,childrenId);
        int no =1;
        if(list.size()>0) {
            for (SalaryBankPayBack salaryBankPayBack : list) {
                salaryBankPayBack.setNo(String.valueOf(no));
                no++;
            }
            if(!"2".equals(queryParam.get("flag"))){
                combined.setNo("合计");
                list.add(0,combined);
            }
        }
        int total = iSalaryBankPayBackDao.getCardNumberFiscalNum(queryParam,childrenId);
        PageDTO<SalaryBankPayBack> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonnelCardNumberFiscal() {
        List<SalaryBankPayBack> salaryBankPayBacks = iSalaryBankPayBackDao.getisLongUse();
        if(salaryBankPayBacks.size()>0){
            for (SalaryBankPayBack salaryBankPayBack : salaryBankPayBacks) {
                iPersonExtDao.updateSalaCardNoByIdenNo(salaryBankPayBack);
                salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                iSalaryBankPayBackDao.updateIsLongUse(salaryBankPayBack);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void undoUpdatePersonnelCardNumberFiscal() {
        List<SalaryBankPayBack> salaryBankPayBacks = iSalaryBankPayBackDao.getisLongUse();
        if(salaryBankPayBacks.size()>0){
            for (SalaryBankPayBack salaryBankPayBack : salaryBankPayBacks) {
                iPersonExtDao.updateSalaCardNoByIdenNo(salaryBankPayBack);
                salaryBankPayBack.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                iSalaryBankPayBackDao.undoUpdateIsLongUse(salaryBankPayBack);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateBankData(String province,List<String> agencyIds) throws AppRuntimeException {
//        查询可以生成数据的人员id
        List<Map<String, Object>> list = iSalaryBankPayBackDao.selectIdAndPersonIdByIsUpdate(agencyIds);
        List<String> ids = list.stream().map(map -> String.valueOf(map.get("id"))).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(ids)){
            throw new AppRuntimeException("没有需要生成的数据。");
        }
        List<String> personIds = list.stream().map(map -> String.valueOf(map.get("personId"))).collect(Collectors.toList());
//        调用文件生成服务
        JSONObject json = generateBankPayBackFile(personIds, province);
        String rscode = String.valueOf(json.get("rscode"));
        if(SystemDictConstant.SUCCESS_CODE.equals(rscode)){
            iSalaryBankPayBackDao.batchUpdateIsRecreateByIds(ids);
        }else{
            String message = String.valueOf(json.get("result"));
            logger.warn("生成异常卡号人员工资数据：{}",message);
            throw new AppRuntimeException(message);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject generateBankPayBackFile(List<String> personIds, String province) throws AppRuntimeException {
        String url = fileServiceConfiguration.getFileservice()+"/v1/salary/generateBankPayBackFile";
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("personIds", String.join(",",personIds));
        paramMap.add("province", province);
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
        JSONObject json = restTemplate.postForEntity(url, httpEntity, JSONObject.class).getBody();
        if(!StringUtils.isEmpty(json) && !StringUtils.isEmpty(json.get("rscode"))){
            return json;
        }else{
            logger.warn("未请求到文件服务或文件服务返回值异常！");
            throw new RuntimeException("请求文件服务失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateAndExportBankData(String province, OutputStream outputStream) throws IOException {
//        查询可以生成数据的人员id
        List<Map<String, Object>> list = iSalaryBankPayBackDao.selectIdAndPersonIdByIsUpdate();
        List<String> ids = list.stream().map(map -> String.valueOf(map.get("id"))).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(ids)){
            throw new AppRuntimeException("没有需要生成的数据。");
        }
        List<String> idenNos = list.stream().map(map -> String.valueOf(map.get("idenNo"))).collect(Collectors.toList());
//        获取汇总数据
        List<Offer> offerList = iSalaryBankPayBackDao.selectHZOffer(idenNos, province);
//        获取银行编码和银行名称
        List<PayeeAcct> payeeAccts = payeeAcctDao.selectBankCodeAndNameByProvince(province);
        Map<String, Sheet> bankJoinOfferMap = new HashMap<>();
        Workbook workbook = new HSSFWorkbook();
        for(PayeeAcct payeeAcct : payeeAccts){
            String salaCardCode = payeeAcct.getPayeeAcctCode();
            String bankName = payeeAcct.getPayeeAcctBankName();
            Sheet sheet = workbook.createSheet(bankName);
            Row row = sheet.createRow(0);
            for(int i = 0; i < 6; i++){
                Cell cell = row.createCell(i, CellType.STRING);
                switch (i){
                    case 0: cell.setCellValue("报盘明细id"); break;
                    case 1: cell.setCellValue("财政编号"); break;
                    case 2: cell.setCellValue("身份证号"); break;
                    case 3: cell.setCellValue("姓名"); break;
                    case 4: cell.setCellValue("金额"); break;
                    case 5: cell.setCellValue("账号"); break;
                }
            }
            bankJoinOfferMap.put(salaCardCode, sheet);
        }
        for(Offer offer : offerList){
            String salaBankCode = offer.getSalaCardBank();
            Sheet sheet = bankJoinOfferMap.get(salaBankCode);
            int rowIndex = sheet.getPhysicalNumberOfRows();
            Row row = sheet.createRow(rowIndex);
            for(int i = 0; i < 6; i++){
                Cell cell = row.createCell(i, CellType.STRING);
                switch (i){
                    case 0: cell.setCellValue(offer.getId()); break;
                    case 1: cell.setCellValue(String.valueOf(offer.getBizKey())); break;
                    case 2: cell.setCellValue(String.valueOf(offer.getIdenNo())); break;
                    case 3: cell.setCellValue(String.valueOf(offer.getPersonName())); break;
                    case 4: cell.setCellValue(String.valueOf(offer.getRealSalary())); break;
                    case 5: cell.setCellValue(String.valueOf(offer.getSalaCardNo())); break;
                }
            }
        }
        workbook.write(outputStream);
        outputStream.close();
        iSalaryBankPayBackDao.batchUpdateIsRecreateByIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pushToPay() {
        List<FilePath> filePaths=filePathDao.selectFilePathConfigByProvince("");
        for(FilePath filePath : filePaths) {
            List<SalaryBankPayBack> bankPayBacks = iSalaryBankPayBackDao.selectNotPushed(filePath.getMofDivCode());
            if (!CollectionUtils.isEmpty(bankPayBacks)) {
                iSalaryBankPayBackDao.updateIsPushed(filePath.getMofDivCode());
                //数据回流接口 file服务无法回调容器

                PayDetailBatch payDetailBatch=new PayDetailBatch();
                payDetailBatch.setMofDivCode(filePath.getMofDivCode());
                payDetailBatch.setDataType("1");
                payDetailBatch.setDatas(new ArrayList<>());
                String year="";
                for(SalaryBankPayBack offer:bankPayBacks){
                    PayDetail payDetail=new PayDetail();
                    payDetail.setPay_detail_id(offer.getId());
                    payDetail.setIden_no(offer.getIdenNo());
                    payDetail.setPer_name(offer.getPersonName());
                    payDetail.setPay_amt( offer.getPayAmt());
                    payDetail.setPayee_acct_no(offer.getOldSalaCardNo());
                    payDetail.setPayee_acct_bank_name(filePath.getBankName());
                    payDetail.setPayee_acct_bank_no(filePath.getBankCode());
                    payDetail.setAgency_code(offer.getAgencyCode());
                    payDetail.setAgency_name(offer.getAgencyName());
                    payDetail.setPay_month(offer.getBizKey().substring(0,7));
                    payDetail.setAdd_word( offer.getBankBackRemark());
                    year=offer.getBizKey().substring(0,4);
                    payDetailBatch.getDatas().add(payDetail);
                }
                payDetailBatch.setFiscalYear(year);
                commonService.payDetailBatchPush(payDetailBatch,year,filePath.getMofDivName());
            }
        }
    }
}
